//
//  ActivityOfThird.m
//  A段项目
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ActivityOfThird.h"
#import "Model.h"
#import "NetWorkingHandler.h"
#import "ActivityFirstCell.h"

#import <MJRefresh.h>
#import "UploadController.h"
#import "ActitySecondCell.h"


@interface ActivityOfThird () <UIImagePickerControllerDelegate,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>


@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) CGFloat height;


@end

@implementation ActivityOfThird

{
    NSInteger num;
}


- (void)dealloc {
    
 
    [_tableView release];
    [_model release];
    
    [super dealloc];
}




- (void)viewDidLoad {
    
    [super viewDidLoad];
    

    
    num = 1;
    
    [self dataArray];
    
    [self handleData];
    
    [self createTableView];
    
    [self addBottomButton];
  
    
  /**上拉刷新. */
 self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
     
     num ++;
     
     [self handleData];
     
     [self.tableView reloadData];
     
 }];
    
#pragma mark --下拉刷新
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        num = 1;
        
        [self handleData];
        
        [self.tableView reloadData];
        
    }];
    
   
    
}


- (NSArray *)dataArray {
    
    if (self.arrModel == nil) {
        
        self.arrModel = [NSMutableArray array];
        
        return self.arrModel;
    }else {
        
    return self.arrModel;
        
    }
}

#pragma mark  ** 获取数据.
- (void)handleData {
    
  
    
    NSString *str = [NSString stringWithFormat:@"http://mapp.tiankong.com/activityImage/list?activityId=%ld&pageNum=%ld&pageSize=30",(long)self.model.id,num];
    
   
    
   [NetWorkingHandler netWorkingWithUrl:str complete:^(id result) {
       
       
       if (result == nil) {
           
           self.tableView.mj_footer.hidden = YES;
           
       }
       
       NSDictionary *dic = [result objectForKey:@"data"];
       NSArray *arr = [dic objectForKey:@"list"];
       
       for (NSDictionary *dic1 in arr) {
           
           Model *modle = [[Model alloc] init];
           
           [modle setValuesForKeysWithDictionary:dic1];
           
           [self.arrModel addObject:modle];
           
           [modle release];
           
       }
       
       [self.tableView reloadData];
       
       [self.tableView.mj_footer endRefreshing];
       
       [self.tableView.mj_header endRefreshing];
    
   }];
   

}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 40) style:UITableViewStylePlain];
    
    self.tableView.separatorStyle = 0;
    
    self.tableView.allowsSelection = NO;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ActivityFirstCell class] forCellReuseIdentifier:@"pool1"];
    
    [self.tableView registerClass:[ActitySecondCell class] forCellReuseIdentifier:@"pool2"];
   
    
    [_tableView release];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
        
        return self.arrModel.count + 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        ActivityFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        [cell passedInfo:self.model];
        
        cell.selectionStyle = 0;
        
        cell.selectionStyle = 0;
        
        return cell;
        
    }else {
        
        Model *modle = [self.arrModel objectAtIndex:indexPath.row - 1];
        
        ActitySecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
      
        
        cell.selectionStyle = 0;
        [cell passedInfo:modle];

       
    
        
       return cell;
    }
    
    
}

#pragma mark --cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
  
    if (indexPath.row == 0) {
        
       
        return   [ActivityFirstCell heightForCell:self.model];
    }
    
  
    
    
    else {
        
        Model *modle = [self.arrModel objectAtIndex:indexPath.row - 1];
        
        return modle.height/modle.width * (self.view.frame.size.width - 10);
        
        

    }
    
    
}


#pragma mark -- 底部 button 的 相关方法
/**创建底部 button. */
- (void)addBottomButton {
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.view addSubview:button];
    
    [button bringSubviewToFront:self.view];
    
    button.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
    
    button.backgroundColor = [UIColor blackColor];
    button.alpha = 0.8;
    
    [button setTitle:@"我也要参加" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [button setImage:[UIImage imageNamed:@"cmera2"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --拍照
- (void)takePhoto {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        picker.allowsEditing = YES;
        
        picker.delegate = self;
        
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:picker animated:YES completion:nil];
        
        [picker release];
    }else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的设备不支持相机" preferredStyle:1];
        
        [self presentViewController:alert animated:NO completion:^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
    }
    
}

/**button 点击事件. */

- (void)buttonAction:(UIButton *)button {
    
    
    
    UIAlertController *AC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0];
    
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"拍照" style:0 handler:^(UIAlertAction * _Nonnull action) {
        
        [self takePhoto];
        
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"发布照片" style:0 handler:^(UIAlertAction * _Nonnull action) {
        
        [self usePhotoLibrary];
        
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [AC addAction:action];
    [AC addAction:action2];
    [AC addAction:action3];
    
    [self presentViewController:AC animated:YES completion:nil];
    
    
    
}

#pragma mark --调用相册

- (void)usePhotoLibrary {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
    [picker release];
    
}


/**选择照片之后的动作. */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    
    UploadController *upload = [[UploadController alloc] init];
    
    [self.navigationController pushViewController:upload animated:YES];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    upload.modalTransitionStyle = 1;
    
    [upload release];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    
    if ([type isEqualToString:@"public.image"]) {
        
        NSData *data = [NSData data];
        
        if (UIImagePNGRepresentation(image)==nil) {
            
            data = UIImageJPEGRepresentation(image, 0);
        }else {
            
            data = UIImagePNGRepresentation(image);
            
        }
        
        //将图片保存到沙盒.
        
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"image.png"];
        
        [data writeToFile:path atomically:YES];
        
        upload.imagePath = path;
        
    }
    
    
    
}




- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}


- (void)viewWillDisappear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = NO;
    

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
